package codingforgreat.lchot150;

public class Lc169 {
    public int majorityElement(int[] arr) {
        int cadit = 0;//候选
        int HP = 0;//血量
        for (int i = 0; i < arr.length; i++) {
            if(HP == 0){
                cadit = arr[i];
                HP = 1;
            }else if(arr[i] == cadit){
                HP++;
            }else {
                HP--;
            }
        }
        if(HP == 0) {
            return 0;
        }
        HP = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == cadit) {
                HP++;
            }
        }
        return cadit;

    }
}
